﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBService.Service
{
    public class AlbumService : RohonModels.IModels.IAlbum, IDisposable
    {
        internal DBService.DBManager db;

        public AlbumService()
        {
            db = new DBManager();
        }

        public int Insert(RohonModels.Models.Album album)
        {
            RohonModels.Models.Album aid = null;
            bool albumIsExist = isExist(album.Name, out aid);
            if (!albumIsExist)
            {
                db.Album.Add(album);
                return Convert.ToInt32(album.Id);
            }
            else
            {
                album = aid;
                return Convert.ToInt32(aid.Id);
            }
        }

        public bool isExist(string albumName, out RohonModels.Models.Album album)
        {
            bool isExist = false;
            var query = db.Album.Where(a => a.Name.Equals(albumName));
            if (query.Count() > 0)
            {
                isExist = true;
                album = query.First();
            }
            else
            {
                album = null;
            }
            return isExist;
        }
        public bool isExist(string albumName, int siteId)
        {
            bool isExist = false;
            var query = db.Album.Where(a => a.Name.Equals(albumName)).Where(a => a.SiteId == siteId);
            if (query.Count() > 0)
            {
                isExist = true;
            }
            return isExist;
        }
        public void Dispose()
        {
            db.SaveChanges();
            db.Dispose();
        }
    }
}
